﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using QRCoder;
using server.Models;
using shenhao.web.Models;

namespace shenhao.web.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";

            return View();
        }

        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }


        public IActionResult QRCodeDownloadURL()
        {
            string downloadUrl = Startup.StaticConfiguration.GetSection("Server").GetSection("Host").Value + "apk/shenhao.apk";
            QRCodeGenerator qR = new QRCodeGenerator();
            QRCodeData codeData = qR.CreateQrCode(downloadUrl, QRCodeGenerator.ECCLevel.H, true);
            QRCoder.QRCode qRCode = new QRCode(codeData);
            var bitmap = qRCode.GetGraphic(5);
            using (MemoryStream ms = new MemoryStream())
            {
                bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                return File(ms.ToArray(), FileContentType.GetMimeType(".bmp"));
            }
        }
    }
}
